package com.mycompany.jfxcurve100;

import chartUtils.ChartSupport;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import mymath.Functions;
import chartUtils.PointList;

public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("JavaFX with Maven");

        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene01.fxml"));
        AnchorPane page = (AnchorPane) root;

        System.out.println(FXMLLoader.FX_CONTROLLER_ATTRIBUTE);

        Pane plotPane = (Pane) page.lookup("#AnchorPane1");

        if (plotPane == null) {
            System.out.println("AnchorPane1 is null!");
        } else {
            System.out.println("AnchorPane1 is not null!");
        }

        LineChart<Number, Number> lineChart = createLineChart();
        
        PointList pl1 = Functions.calcXYPointListForFunction(Functions.func01, 0, 10, 50);
        String functionName = "func01";
        lineChart = ChartSupport.addSeriesToXYChart(pl1, functionName, lineChart);        
        
        functionName = "func03";
        PointList pl2 = Functions.calcXYPointListForFunction(Functions.func03, 0, 12, 50);
        lineChart = ChartSupport.addSeriesToXYChart(pl2, functionName, lineChart);        
        
        functionName = "factorial";
        PointList pl3 = Functions.calcXYPointListForFunction(Functions.factorial, 0, 3, 50);
        lineChart = ChartSupport.addSeriesToXYChart(pl3, functionName, lineChart);
        
        
        VBox lineChartInVBoxLayout = ChartSupport.createVBoxLayoutForChart(lineChart);

        Label cursorCoords = ChartSupport.createCursorGraphCoordsMonitorLabel(lineChart);

        lineChartInVBoxLayout = ChartSupport.addCursorCoords(lineChartInVBoxLayout, cursorCoords);

        plotPane.getChildren().add(lineChartInVBoxLayout);
        plotPane.getStyleClass().add("pane");

        Scene scene = new Scene(page);
        stage.setScene(scene);
        scene.getStylesheets().add("/styles/Styles.css");

        stage.show();

    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifactorialts, e.g., in IDEs with limited
     * FX support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    
    
    private LineChart<Number, Number> createLineChart() {
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        //FIXME: manual formatting of XAxis
        xAxis.setAutoRanging(true);
        //xAxis.setLowerBound(3);
        //xAxis.setUpperBound(8);
        xAxis.setTickUnit(1);
        //to disable the minor ticks for the horizontal axis
        xAxis.setMinorTickCount(0);

        LineChart<Number, Number> lineChart
                = new LineChart<>(xAxis, yAxis);
        lineChart.setTitle("Function plot");

        //just some other stuff here
        System.out.println(Functions.fibonacci(4) + " /" + Functions.fibonacci(8));
        System.out.println(Functions.factorial.apply(4.0) + " /" + Functions.factorial.apply(5.0));

        return lineChart;
    }

}
